# coding=utf-8
import maya.cmds as cmds
import maya.mel as mel
import os
import pipeline
#import CommonModule.TimeControl as TmC
import CommonModule
import core as timelimit
class Calculating:

	def Win_Min(self):
		if timelimit.ProductActivation().Win() == 0:
			return
		UI_Name= u"控制器定点"
		if cmds.window(UI_Name,q=True,ex=True):
			cmds.deleteUI(UI_Name)
		cmds.window( UI_Name,menuBar=True,widthHeight=[400,200],sizeable= 0)
		
		cmds.columnLayout(adj = 1,h=200,w = 400)
		cmds.menuBarLayout(bgc=(0,0,0),h= 25,w = 400)
		cmds.menu( label=u'帮助' )
		cmds.menuItem(label=u'教程', c = lambda *args:self.Demo())
		cmds.setParent( '..' )
		
		cmds.rowLayout( numberOfColumns=4, h = 35,columnWidth4=(100, 100, 100, 100), columnAttach=[(1, 'both', 5), (2, 'both', 5), (3, 'both', 5), (4, 'both', 5)])
		cmds.button(l = u'初始化场景',c = lambda *args:self.Delete_History())
		self.CreateControl_button = cmds.button(l = u'创建控制')
		cmds.button( self.CreateControl_button,e = 1,c = lambda *args:self.CreateControl())
		self.SelControl_button = cmds.button(l = u'选择控制器')
		cmds.button(self.SelControl_button,e = 1,c = lambda *args:self.SelControl())
		self.AddControl_button = cmds.button(l = u'添加控制')
		cmds.button(self.AddControl_button,e = 1,c = lambda *args:self.AddControl_Win())
		cmds.setParent ('..')
		
		cmds.separator(height=20,style="doubleDash")
		
		cmds.flowLayout( h = 20,w = 400)
		self.KeysRadioButtonGrp = cmds.radioButtonGrp( numberOfRadioButtons=2, select = 1,label=u'帧模式', labelArray2=[u'拷贝', u'烘焙'],annotation1 = 'Copy',annotation2= 'Bake')
		cmds.radioButtonGrp(self.KeysRadioButtonGrp,e= 1,onCommand1 = lambda *args:self.Keys_Copy_RadioButtonGrp_Min(),onCommand2 = lambda *args:self.Keys_Bake_RadioButtonGrp_Min())
		cmds.setParent ('..')
		
		cmds.separator(height=20,style="doubleDash")
		
		cmds.flowLayout( h = 20,w = 400)
		self.TimesRadioButtonGrp = cmds.radioButtonGrp( numberOfRadioButtons=2, label=u'时间帧烘焙范围', labelArray2=[u'当前时间滑条', u'开始/结束'],visible=0)
		cmds.setParent ('..')
		
		cmds.separator(height=20,style="doubleDash")
		
		cmds.columnLayout(h = 30,w = 400)
		self.Ok_Button = cmds.button(l = u'确定',w = 400,h = 25)
		cmds.button(self.Ok_Button,e =1,c=lambda *args:self.Ok_Button_Min())
		cmds.showWindow()

	def Demo(self):
		print
		os.startfile(pipeline.PythonRootPath()+'/help/ani/CenterAuxiliary.gif')

	def Delete_History(self):
		All = cmds.ls() 
		for AllList in All:
			if 'MobileGroup'or'MobileLoc' in AllList:
				cmds.delete('MobileGroup')

	def Keys_Copy_RadioButtonGrp_Min(self):
		cmds.radioButtonGrp( self.TimesRadioButtonGrp , e= 1,visible=0)
	def Keys_Bake_RadioButtonGrp_Min(self):
		cmds.radioButtonGrp( self.TimesRadioButtonGrp , e= 1,visible=1)
	def Warning_Win(self):
		cmds.confirmDialog(message = u'需要选择一个控制器')	   
#创建
	def CreateControl(self):
		self.selObject = cmds.ls(sl=1)
		if len(self.selObject) == 0:
			self.Warning_Win()
			return
		#self.MaxTimes=cmds.playbackOptions( q =1,maxTime=1)
		#self.MinTimes=cmds.playbackOptions( q=1, minTime=1)
		
		self.MobileGroup = cmds.group(em=True,name='MobileGroup')
		self.MobileLoc = cmds.spaceLocator(name='MobileLoc')
		cmds.parent( self.MobileLoc,  self.MobileGroup)
		cmds.parentConstraint(self.selObject,self.MobileGroup, weight= 1)
		cmds.parentConstraint(self.selObject, self.MobileGroup,e= 1,remove = 1)
		cmds.button( self.SelControl_button,e = 1,c = lambda *args:self.SelControl(self.MobileLoc))
		
#选择控制
	def SelControl(self,MobileLoc):
		self.selMobileLoc = cmds.select(MobileLoc) 
	def AddControl_Win(self):
		GetObjectTime= cmds.keyframe(self.selObject, query=True, timeChange=True )
		cmds.parentConstraint( self.selObject,self.MobileLoc, mo = 1,weight= 1)
		cmds.bakeResults(self.MobileLoc,simulation= 0 ,t=(min(GetObjectTime),max(GetObjectTime)) ,smart =1 ,disableImplicitControl =1 ,preserveOutsideKeys =1 ,sparseAnimCurveBake= 0,removeBakedAttributeFromLayer =0,removeBakedAnimFromLayer =0,bakeOnOverrideLayer =0 ,minimizeRotation =1 ,controlPoints =0 ,shape=1 )
		cmds.parentConstraint( self.MobileLoc,self.selObject, mo = 1,weight= 1)

	def Ok_Button_Min(self):
		GetMobileLocTime = cmds.keyframe(self.MobileLoc, query=True, timeChange=True)
		Get_select =  cmds.radioButtonGrp(self.KeysRadioButtonGrp,q=1,select=1)
		if Get_select==1:
			cmds.bakeResults(self.selObject,simulation= 0 ,t=(min(GetMobileLocTime),max(GetMobileLocTime)),smart =1 ,disableImplicitControl =1 ,preserveOutsideKeys =1 ,sparseAnimCurveBake= 0,removeBakedAttributeFromLayer =0,removeBakedAnimFromLayer =0,bakeOnOverrideLayer =0 ,minimizeRotation =1 ,controlPoints =0 ,shape=1 )
			#print '11111111111111111111111111'
		   
		else:
			Get_TimesRadio = cmds.radioButtonGrp(self.TimesRadioButtonGrp,q=1,select=1)
			if Get_TimesRadio ==1:
				cmds.bakeResults(self.selObject,t=(min(GetMobileLocTime),max(GetMobileLocTime)),simulation= 1,oversamplingRate = 1,disableImplicitControl =1 ,preserveOutsideKeys =1 ,sparseAnimCurveBake= 0,removeBakedAttributeFromLayer =0,removeBakedAnimFromLayer =0,bakeOnOverrideLayer =0 ,minimizeRotation =1 ,controlPoints =0 ,shape=1 )
				#print '2222222222222222222222222222'
			else: 
				aPlayBackSliderPython = mel.eval('$tmpVar=$gPlayBackSlider')
				times=  cmds.timeControl (aPlayBackSliderPython,q=1 ,rangeArray= 1)
				#print '33333333333333333333333'
				cmds.bakeResults(self.selObject,t=(int(times[0]),int(times[1])-1),simulation= 1,oversamplingRate = 1,disableImplicitControl =1 ,preserveOutsideKeys =1 ,sparseAnimCurveBake= 0,removeBakedAttributeFromLayer =0,removeBakedAnimFromLayer =0,bakeOnOverrideLayer =0 ,minimizeRotation =1 ,controlPoints =0 ,shape=1 )
		cmds.delete(self.MobileGroup)  
#Calculating().Win_Min()